package com.ds.common.org.expression.org;

import java.util.ArrayList;
import java.util.List;

import com.ds.common.expression.ParseException;
import com.ds.common.expression.function.AbstractFunction;
import com.ds.org.Org;
import com.ds.org.OrgManager;
import com.ds.server.OrgManagerFactory;

/**
 * 取得指定的一些组织的下级组织（包括直接的和间接的），包含去重功能。
 * <p>
 * Title: JDSORG组织机构中间件
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: jds Copyright (c) 2003-2008
 * </p>
 * <p>
 * Company: www.justdos.net
 * </p>
 *
 * @author wenzhangli
 * @version 2.0
 */
public class GetAllChildrenOrgs extends AbstractFunction {

    public Org[] perform(Object param1) throws ParseException {
        if (param1 == null) {
            return new Org[0];
        }
        // 组织机构数组
        if (param1 instanceof Org[]) {
            Org[] orgs = (Org[]) param1;
            OrgManager accountManager = OrgManagerFactory.getOrgManager();
            List result = new ArrayList();
            for (int i = 0; i < orgs.length; i++) {
                if (orgs[i] != null) {

                    List<Org> orglist = orgs[i].getChildrenRecursivelyList();
                    Org[] os = orglist.toArray(new Org[orglist.size()]);

                    for (int j = 0; j < os.length; j++) {
                        Org org = os[j];
                        if (org != null && !result.contains(org)) {
                            result.add(org);
                        }
                    }
                }
            }
            return (Org[]) result.toArray(new Org[result.size()]);
        }

        throw new ParseException("Invalid parameter type in:" + this.getClass().getName() + ", param1 is " + param1.getClass().getName());
    }
}